<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Http\Models\Banner;

class BannerController extends Controller{

    public function index(Request $request)
    {
        //data不可数
        $data = Banner::orderBy("created_at","desc");
        $data = $data->paginate(20);

        return view("admin.banner.index",['data'=>$data,'active'=>'banner']);
    }

    public function create(Request $request)
    {
        return view('admin.banner.create',['active'=>'banner']);
    }

    public function store(Request $request){
        $banner = new Banner;
        $banner -> title = $request->input('title');
        $banner -> url = $request -> input('url');
        $banner -> cover = $request ->input('cover');
        $banner -> save();
        return redirect('/admin/banner');
    }

    public function edit($id){

        $banner = Banner::find($id);

        return view('admin.banner.edit',['banner'=>$banner]);
    }


    public function update(Request $request,$id)
    {
        $banner = Banner::find($id);

        $banner -> title = $request->input('title');
        $banner -> url = $request -> input('url');
        $banner -> cover = $request ->input('cover');

        $banner->save();

        return redirect('/admin/banner');
    }

    public function delete($id)
    {
        $res = Banner::destroy($id);
        if($res) return view('success',['url'=>'/admin/banner']);

        return view('failure',['url'=>'/admin/banner']);
    }

}